package com.yuda.tools.authreport.controller;

import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.Sftp;
import com.jcraft.jsch.Session;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.tools.authreport.domain.vo.OrderExcel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/excelNoPdf")
public class ExcelOrderNoPdf {


    /**
     * window 端使用 的工具
     *
     * @throws Exception
     */
    @GetMapping("find")
    public void find() throws Exception {
        File file = new File("C:\\Users\\AI_Win10\\Desktop\\订单列表 0913.xlsx");
        ExcelUtil<OrderExcel> util = new ExcelUtil<OrderExcel>(OrderExcel.class);
        List<OrderExcel> orderExcels = util.importExcel(new FileInputStream(file));
        Session session = JschUtil.getSession("182.92.96.88", 22, "root", "Thussat@200518");

        List<List<OrderExcel>> collect = orderExcels.stream().collect(Collectors.groupingBy(x -> x.getExamUuid() + "_" + x.getIdCard())).values().stream().filter(x -> x.size() > 1).collect(Collectors.toList());
        System.out.println(collect);

        int i = 0;

        try (Sftp sftp = JschUtil.createSftp(session)) {
            System.out.println("session.isConnected() ===  >  " + session.isConnected());
            for (OrderExcel orderExcel : orderExcels) {
                System.out.println(++i);
                String fileName = "/mnt/auth_pdf/download/20219131526/" + (orderExcel.getExamUuid().contains("TACA") ? orderExcel.getExamUuid() : "TDA") + orderExcel.getProvince() + orderExcel.getName() + orderExcel.getIdCard().substring(orderExcel.getIdCard().length() - 6) + ".pdf";
                boolean exist = sftp.exist(fileName);
                if (!exist) {
                    System.out.println(fileName);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            session.disconnect();

        }
    }

}
